package com.nstar.orderexpress.view
{
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.nstar.base.controls.HintTextInput;
	import com.nstar.base.util.Logger;
	import com.nstar.orderexpress.control.SearchEvent;
	import com.nstar.orderexpress.model.Model;
	import com.nstar.orderexpress.vo.Menu2RestVO;
	import com.nstar.orderexpress.vo.OrderMenuVO;
	import com.nstar.orderexpress.vo.PaginatorVO;
	import com.nstar.orderexpress.vo.RequestVO;
	import com.nstar.orderexpress.vo.RestInfoVO;
	
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Button;
	import mx.controls.CheckBox;
	import mx.controls.HSlider;
	import mx.controls.Label;
	import mx.controls.RadioButtonGroup;
	import mx.events.FlexEvent;
	import mx.events.ItemClickEvent;
	import mx.events.SliderEvent;
	
	public class SearchViewClass extends Canvas
	{
		public var btnSearch:Button;
		public var filterSlider:HSlider;
		public var lblFilterValue:Label;
		public var chkLike:CheckBox;
		public var htiSearch:HintTextInput;
		public var rbgSearchType:RadioButtonGroup;
		
		private var model:Model = Model.getInstance();
		private var logger:Logger = Logger.getInstance();
		
		public function SearchViewClass()
		{
			//TODO: implement function
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			btnSearch.addEventListener(MouseEvent.CLICK,searchHandler);
			filterSlider.addEventListener(SliderEvent.CHANGE,changeHandler);
			rbgSearchType.addEventListener(ItemClickEvent.ITEM_CLICK,itemclickHandler);
		}
		
		private function searchHandler(event:MouseEvent):void{
			var request:RequestVO = generateVO();
			
			var e:SearchEvent = new SearchEvent(request);
			CairngormEventDispatcher.getInstance().dispatchEvent(e);
		}
		
		private function itemclickHandler(event:ItemClickEvent):void{
			switch (event.index){
				case 0:
					htiSearch.defaultText = "please input menu name";
					break;
				case 1:
					htiSearch.defaultText = "please input rest name";
					break;
				case 2:
					htiSearch.defaultText = "please input where are you";
					break;
			}
			htiSearch.setDefaultText(null);
		}		
		
		//生成查询VO
		private function generateVO():RequestVO{
			var request:RequestVO = new RequestVO();
			request.isLike = chkLike.selected?"1":"0";//是否模糊查询
			
			var mr:Menu2RestVO = new Menu2RestVO();
			mr.orderMenu = new  OrderMenuVO();
			mr.restInfo = new RestInfoVO();
			
			
			var paginator:PaginatorVO = new PaginatorVO();
			paginator.pageNo = 1; 
			paginator.pageSize = 15;
			
			request.menu2Rest = mr;
			request.paginator = paginator;
			
			switch (rbgSearchType.selectedValue){
				case Constants.SEARCH_TYPE_MENU:
					mr.orderMenu.name = htiSearch.text;
					break;	
				case Constants.SEARCH_TYPE_REST:
					mr.restInfo.name = htiSearch.text;
					break;
				case Constants.SEARCH_TYPE_OTHER:
					break;
			}
			request.searchType = rbgSearchType.selectedValue.toString();
			model.addModel("request",request);
			return request;
		}		

		//条件过滤回调方法
		private function filterCallback(item:Object):Boolean{
			//return item.orderMenu.price<filterSlider.value;
			return item.price<filterSlider.value;
		}
		
		//数据过滤
		private function changeHandler(event:SliderEvent):void{
			model.mainApp.searchResultView.atlResult.filter(filterCallback,true);
			lblFilterValue.text = "Filter Value : ￥"+event.value;
		}
		
	}
}